package com.ziqikeji.xianyan.controller.tag;

import com.ziqikeji.xianyan.common.DatagridDTO;
import com.ziqikeji.xianyan.entity.tag.ArticleTag;
import com.ziqikeji.xianyan.service.tag.ArticleTagService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 标签 controller
 * @author liuyu
 */
@Controller
@RequestMapping(value = "admin/articleTag")
public class TagController {

    @Autowired
    private ArticleTagService articleTagService;
    
    @RequestMapping(value = "/index", method = RequestMethod.GET)
    public String hello() {
        return "/admin/tag/listAll";
    }

    /**
     * 获取全部列表
     * @return data dto
     */
    @ResponseBody
    @RequestMapping(value = "getAll")
    public DatagridDTO datagrid() {
        List<ArticleTag> articleTagList = this.articleTagService.getAllNoDelete();
        if (null != articleTagList) {
            return new DatagridDTO("列表获取成功", 0, articleTagList.size(), articleTagList);
        } else {
            return new DatagridDTO("列表获取成功", 0, 0, null);
        }
    }

    /**
     * 获取分页列表
     * @return data dto
     */
    @ResponseBody
    @RequestMapping(value = "datagrid")
    public DatagridDTO getPage(@RequestParam(value = "pageNumber", defaultValue = "1") String pageNumber,
                               @RequestParam(value = "pageSize", defaultValue = "10") String pageSize) {
        try {
            int page = 0;
            int size = 10;
            if (!StringUtils.isEmpty(pageNumber)) {
                page = Integer.parseInt(pageNumber) - 1;
                page = Math.max(page, 0);
            }
            if (!StringUtils.isEmpty(pageSize)) {
                size = Integer.parseInt(pageSize);
                size = Math.max(size, 0);
            }
            Page<ArticleTag> articleTagPage = this.articleTagService.getPage(page, size);
            return new DatagridDTO("列表获取成功", 0, articleTagPage.getTotalElements(), articleTagPage.getContent());
        } catch (Exception e) {
            return new DatagridDTO("获取失败(错误信息：" + e.getMessage() + "）", 1, null);
        }
    }

    /**
     * 进入添加页面
     * @param model
     * @return
     */
    @RequestMapping("/add")
    public String add(Model model) {
        ArticleTag articleTag = new ArticleTag();
        model.addAttribute("articleTag", articleTag);
        model.addAttribute("action", "save");
        return "admin/tag/edit";
    }

    /**
     * 通过ID获取
     * @param id
     * @return
     */
    @RequestMapping(value = "get/{id}", method = RequestMethod.GET)
    public ArticleTag get(@PathVariable("id") String id) {
        return this.articleTagService.get(id);
    }


    /**
     * 保存
     * @param articleTag
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "save", method = RequestMethod.POST)
    public ResponseEntity<String> save(@ModelAttribute ArticleTag articleTag) {
        try {
            this.articleTagService.save(articleTag);
        } catch (Exception e) {
            return new ResponseEntity<>("保存失败", HttpStatus.INTERNAL_SERVER_ERROR);
        }
        return new ResponseEntity<>("保存成功", HttpStatus.OK);
    }

    /**
     * 进入修改页面
     * @param id
     * @param model
     * @return
     */
    @RequestMapping(value = "modify/{id}", method = RequestMethod.GET)
    public String modify(@PathVariable("id") String id, Model model) {
        ArticleTag articleTag = articleTagService.get(id);
        model.addAttribute("articleTag", articleTag);
        model.addAttribute("action", "update");
        return "admin/tag/edit";
    }

    /**
     * 修改更新
     * @param articleTag
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "update", method = RequestMethod.POST)
    public ResponseEntity<String> update(@ModelAttribute ArticleTag articleTag) {
        try {
            this.articleTagService.update(articleTag);
            return new ResponseEntity<>("修改成功", HttpStatus.OK);
        } catch (Exception e) {
            return new ResponseEntity<>("修改失败", HttpStatus.INTERNAL_SERVER_ERROR);
        }
    }

    /**
     * 进入修改页面
     * @param id
     * @param model
     * @return
     */
    @RequestMapping(value = "detail/{id}", method = RequestMethod.GET)
    public String view(@PathVariable("id") String id, Model model) {
        ArticleTag articleTag = articleTagService.get(id);
        model.addAttribute("articleTag", articleTag);
        return "admin/tag/detail";
    }

    /**
     * 删除
     * @param id
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "delete/{id}", method = RequestMethod.POST)
    public ResponseEntity<String> delete(@PathVariable("id") String id) {
        try {
            this.articleTagService.deleteMark(id);
            return new ResponseEntity<>("删除成功", HttpStatus.OK);
        } catch (Exception e) {
            return new ResponseEntity<>("删除失败", HttpStatus.INTERNAL_SERVER_ERROR);
        }
    }

    /**
     * 所有RequestMapping方法调用前的Model准备方法, 实现Struts2 Preparable二次部分绑定的效果,先根据form的id从数据库查出Organize对象,再把Form提交的内容绑定到该对象上。
     * 因为仅update()方法的form中有id属性，因此仅在update时实际执行.
     */
    @ModelAttribute
    public void get(@RequestParam(value = "articleTag.id", defaultValue = "-1") String id, Model model) {
        if (!"-1".equals(id)) {
            ArticleTag articleTag = this.articleTagService.get(id);
            model.addAttribute("articleTag", articleTag);
        }
    }
}
